<?php
/*
 * 后台分类控制器
 * @author     Leo
 * @Created  2016/6/17
 */
namespace Admin\Controller;
// use Think\Controller;
class CategoryController extends CommonController {
     protected $cate;
     public function __construct(){
          parent::__construct();
          $this->cate=D('Cate');
     }
     
     public function index(){
          $cats=$this->cate->getTree();
          $this->assign('cats',$cats);
          $this->display();
     }
     
     public function getTree(){
          $cats = $this->cate->getTree();
          echo json_encode($cats);
          exit;
     }
     //添加分类
     public function add(){
          if(IS_POST){
               $_POST['addtime'] = time();
               $this->cate->add($_POST);
               exit($this->success('添加成功!','/Admin/Category/index'));
          }else{
               $cats=$this->cate->getTree();
               $this->assign('cats',$cats);
               $this->display();
          }
     }
     //修改分类
     public function edit(){
          $id = I('get.id');
          $cateone = $this->cate->where(['cat_id'=>$id])->find();
          if(IS_POST){
               $this->cate->where(['cat_id'=>$id])->save($_POST);
               exit($this->success('修改成功!','/Admin/Category/index'));
          }else{
               $cates = $this->cate->select();
               //print_r($cates);exit;
               $this->assign('cates',$cates);
               $this->assign('cate',$cateone);
               $this->display();
          }
     }
     
     //删除分类
     public function del(){
          $id = I('get.id');
          $res=$this->cate->where(['cat_id'=>$id])->delete();
          if($res){
               //删除成功后删除当前分类下的所有子分类
               $this->cate->where(['parent_id'=>$id])->delete();
               exit($this->success('删除成功!','/Admin/Category/index'));
          }else{
               exit($this->error('删除失败!'));
          }
     }
}
